<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>法律法规的发布与管理</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
</head>
<body>

<div class="container my-5">
    <h2 style="text-align: center;">法律法规管理</h2>
    <table class="table">
        <thead>
        <tr>
            <th>编号</th>
            <th>标题</th>
            <th>发布日期</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="lawsTableBody">
        <!-- 动态加载数据 -->
        </tbody>
    </table>
    <button type="button" class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#addModal" style="float:right;">
        新增法规
    </button>
    <!-- 添加法规 Modal -->
    <div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">新增法律法规</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form id="addForm">
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="title" class="form-label">标题</label>
                            <input type="text" class="form-control" id="title" required>
                        </div>
                        <div class="mb-3">
                            <label for="publishDate" class="form-label">发布日期</label>
                            <input type="date" class="form-control" id="publishDate" required>
                        </div>
                        <div class="mb-3">
                            <label for="detailContent" class="form-label">详细内容</label>
                            <textarea class="form-control" id="detailContent" rows="3" required></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                        <button type="button" onclick="addLaw()" class="btn btn-primary">保存</button>
                    </div>
                </form>
            </div>
        </div>
    </div>


</div>

<script >
    function fetchLaws() {
        $.ajax({
            url: '/api/laws',
            type: 'GET',
            success: function (data) {
                let tableBody = $('#lawsTableBody');
                tableBody.empty(); // 清空现有数据，准备填充新的数据

                // 遍历数据并生成表格行
                data.forEach(function (law) {
                    let row = `<tr>
                                <td>${law.lawId}</td>
                                <td>${law.title}</td>
                                <td>${law.publishDate}</td>
                                <td>
                                    <button class="btn btn-sm btn-warning edit-btn" data-id="${law.lawId}">编辑</button>
                                    <button class="btn btn-sm btn-danger delete-btn" data-id="${law.lawId}" onclick="deleteLaw(${law.lawId})">删除</button>
                                </td>
                            </tr>`;
                    tableBody.append(row); // 将生成的行追加到表格中
                });
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.error("获取法律法规失败: ", textStatus, errorThrown);
            }
        });
    }


        // 添加法规提交处理
        function addLaw() {
            let title = $('#title').val();
            let publishDate = $('#publishDate').val();
            let detailContent = $('#detailContent').val();

            $.ajax({
                url: '/api/laws',
                type: 'POST',
                data: JSON.stringify({
                    title: title,
                    publishDate: publishDate,
                    detailContent: detailContent
                }),
                contentType: 'application/json',
                success: function () {
                    $('#addModal').modal('hide');
                    fetchLaws(); // 刷新表格数据
                }
            });
        }

        // 编辑专利
        function editlaw(lawId) {
            alert('编辑专利功能尚未实现');
        }


        // 删除专利
        function deleteLaw(lawId) {
            $.ajax({
                url: '/api/laws/' + lawId,
                type: 'DELETE',
                success: function () {
                    fetchLaws();
                }
            });
        }

    // 页面加载时初始化专利信息列表
    // 初始化页面时获取专利信息并显示在表格中
    $(document).ready(function() {
        fetchLaws();

});

</script>

</body>
</html>